package com.example.demo.user; // 确保包名正确

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class LoginController {

    @GetMapping("/")
    public String home() {
        // 检查用户是否已登录
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.isAuthenticated() &&
            !authentication.getName().equals("anonymousUser")) {
            return "redirect:/dashboard"; // 已登录用户跳转到主页
        }
        return "redirect:/login"; // 未登录用户跳转到登录页面
    }

    @GetMapping("/login")
    public String showLoginPage() {
        // 检查用户是否已登录
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.isAuthenticated() &&
            !authentication.getName().equals("anonymousUser")) {
            return "redirect:/dashboard"; // 已登录用户直接跳转到主页
        }
        return "user/register"; // 指向你的合并登录注册页面
    }

    @GetMapping("/user/register")
    public String showRegisterPage() {
        return "user/register"; // 显示注册页面
    }
}